﻿using BattleFury.Lib.Managers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BattleFury.Lib.Input
{
	public class MouseManager
		: Manager
	{
		private MouseState _previous;
		private MouseState _current;

		public Vector2 CurrentLocation { get; set; }
		public Vector2 PreviousLocation { get; set; }

		public Rectangle CurrentBounds
		{
			get
			{
				return new Rectangle((int)CurrentLocation.X, (int)CurrentLocation.Y, 1, 1);
			}
		}

		public Rectangle PreviousBounds
		{
			get
			{
				return new Rectangle((int)PreviousLocation.X, (int)PreviousLocation.Y, 1, 1);
			}
		}

		public MouseManager()
			: base()
		{
		}

		public override void Update(GameTime gameTime)
		{
			if (!IsEnabled) return;

			_previous = _current;
			_current = Mouse.GetState();

			PreviousLocation = CurrentLocation;
			CurrentLocation = new Vector2(_current.X, _current.Y);
		}

		public bool IsMouseButtonPressed(MouseButtons button)
		{
			switch (button)
			{
				case MouseButtons.Left: return _current.LeftButton == ButtonState.Pressed && _previous.LeftButton == ButtonState.Released;
				case MouseButtons.Middle: return _current.MiddleButton == ButtonState.Pressed && _previous.MiddleButton == ButtonState.Released;
				case MouseButtons.Right: return _current.RightButton == ButtonState.Pressed && _previous.RightButton == ButtonState.Released;
				case MouseButtons.X1: return _current.XButton1 == ButtonState.Pressed && _previous.XButton1 == ButtonState.Released;
				case MouseButtons.X2: return _current.XButton2 == ButtonState.Pressed && _previous.XButton2 == ButtonState.Released;
				default: return false;
			}
		}

		public bool IsMouseButtonHeld(MouseButtons button)
		{
			switch (button)
			{
				case MouseButtons.Left: return _current.LeftButton == ButtonState.Pressed && _previous.LeftButton == ButtonState.Pressed;
				case MouseButtons.Middle: return _current.MiddleButton == ButtonState.Pressed && _previous.MiddleButton == ButtonState.Pressed;
				case MouseButtons.Right: return _current.RightButton == ButtonState.Pressed && _previous.RightButton == ButtonState.Pressed;
				case MouseButtons.X1: return _current.XButton1 == ButtonState.Pressed && _previous.XButton1 == ButtonState.Pressed;
				case MouseButtons.X2: return _current.XButton2 == ButtonState.Pressed && _previous.XButton2 == ButtonState.Pressed;
				default: return false;
			}
		}
	}
}